package edu.yctc.genesis.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import edu.yctc.genesis.entity.ResultDO;
import edu.yctc.genesis.entity.UserDO;
import edu.yctc.genesis.service.UserIService;
import edu.yctc.genesis.constant.ResultCode;
import edu.yctc.genesis.dao.UserDAO;

@ComponentScan({ "edu.yctc.genesis.dao" })
@Service("userService")
public class UserServiceImpl implements UserIService {

    @Resource
    private UserDAO userDAO;

    @Override
    public ResultDO<Void> signup(UserDO userDO) {

        // 0. 参数校验
        if (StringUtils.isBlank(userDO.getUsername()) || StringUtils.isBlank(userDO.getPassword())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }

        try {
            // 1. 插入数据库
            userDAO.insert(userDO);
        } catch (Exception e) {
            // TODO log

            return new ResultDO<>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR);
        }

        // TODO log
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

}
